import { mapServer } from "../server";


class Query {
    /**
     * 点查询
     * @param options
     * @param options.position 点的坐标数组[x,y]
     * @param options.service = {
     *     docName: "xzd2404", //地图文档名称
     *     layerId: 3 //图层索引
     * } 地图文档查询服务对象
     */
    static queryByPnt({
        position,
        service,
        // success
    }) {
        //创建一个用于查询的点形状
        let pointObj = new Zondy.Object.Point2D(position[0], position[1]);
        //设置查询点的搜索半径
        /* 如果zoom太小扩大搜索半径 */
        pointObj.nearDis = 1;
        //1、初始化查询结构对象，设置查询结构包含几何信息
        var queryStruct = new Zondy.Service.QueryFeatureStruct();
        //是否包含几何图形信息
        queryStruct.IncludeGeometry = true;
        //是否包含属性信息
        queryStruct.IncludeAttribute = true;
        //是否包含图形显示参数
        queryStruct.IncludeWebGraphic = false;
        //2、指定查询规则
        var rule = new Zondy.Service.QueryFeatureRule({
            //是否将要素的可见性计算在内
            EnableDisplayCondition: false,
            //是否完全包含
            MustInside: false,
            //是否仅比较要素的外包矩形
            CompareRectOnly: false,
            //是否相交
            Intersect: true,
        });
        //3、实例化查询参数对象
        var queryParam = new Zondy.Service.QueryParameter({
            geometry: pointObj,
            resultFormat: "json",
            struct: queryStruct,
            rule: rule,
        });
        //4、实例化地图文档查询服务对象
        var queryService = new Zondy.Service.QueryDocFeature(
            queryParam,
            service.docName,
            service.layerId,
            {}
        );
        //执行查询操作，querySuccess为查询回调函数
        queryService.query((data) => {
            // console.log(data, "query success");

            //将MapGIS要素JSON反序列化为ol.Feature类型数组
            var format = new Zondy.Format.PolygonJSON();
            var features = format.read(data);

            success(data); //查询成功回调函数
        });
    }

    /**
     * geometry查询
     * @param options
     * @param options.geometry 点的坐标数组[x,y]
     * @param options.service = {
     *     docName: "xzd2404", //地图文档名称
     *     layerId: 3 //图层索引
     * } 地图文档查询服务对象
     */
    static queryByGeometry({ geometry, service }) {
        //1.设置查询规则
        //初始化查询结构对象，设置查询结构包含几何信息
        var queryStruct = new Zondy.Service.QueryFeatureStruct();
        //是否包含几何图形信息
        queryStruct.IncludeGeometry = true;
        //是否包含属性信息
        queryStruct.IncludeAttribute = true;
        //是否包含图形显示参数
        queryStruct.IncludeWebGraphic = false;
        //创建一个用于查询的区
        var geomObj = new Zondy.Object.Polygon();
        geomObj.setByOL(geometry);

        ///2.指定查询规则
        var rule = new Zondy.Service.QueryFeatureRule({
            //是否将要素的可见性计算在内
            EnableDisplayCondition: false,
            //是否完全包含
            MustInside: false,
            //是否仅比较要素的外包矩形
            CompareRectOnly: false,
            //是否相交
            Intersect: true,
        });
        //3.实例化查询参数对象
        var queryParam = new Zondy.Service.QueryParameter({
            geometry: geomObj,
            resultFormat: "json",
            rule: rule,
            struct: queryStruct,
        });
        //设置查询分页号
        queryParam.pageIndex = 0;
        //设置查询要素数目
        queryParam.recordNumber = 1000;
        //4.实例化地图文档查询服务对象
        var queryService = new Zondy.Service.QueryDocFeature(
            queryParam,
            service.docName,
            service.layerId,
            mapServer,
        );
        //执行查询操作，querySuccess为查询回调函数
        return new Promise((resolve) => {
            queryService.query((data) => {
                var format = new Zondy.Format.PolygonJSON();
                //将MapGIS要素JSON反序列化为ol.Feature类型数组
                var features = format.read(data);
                resolve(features);
            });
        });
    }

    /**
     * 根据要素查询
     * @param options
     * @param options.service = {
     *     docName: "xzd2404", //地图文档名称
     *     layerId: [1,2,3] //图层索引数组
     * } 地图文档查询服务对象
     */

    static queryByLayerId(service) {
        //1、初始化查询结构对象，设置查询结构包含几何信息
        var queryStruct = new Zondy.Service.QueryFeatureStruct();
        //是否包含几何图形信息
        queryStruct.IncludeGeometry = true;
        //是否包含属性信息
        queryStruct.IncludeAttribute = true;
        //是否包含图形显示参数
        queryStruct.IncludeWebGraphic = false;
        //实例化查询参数对象
        var queryParam = new Zondy.Service.QueryParameter({
            resultFormat: "json",
            struct: queryStruct,
        });
        //设置查询分页号
        queryParam.pageIndex = 0;
        //设置查询要素数目
        queryParam.recordNumber = 1000;
        //实例化地图文档查询服务对象
        var queryService = new Zondy.Service.QueryDocFeature(
            queryParam,
            service.docName,
            service.layerId,
            {}
        );
        //执行查询操作，querySuccess为查询回调函数
        return new Promise((resolve) => {
            queryService.query((data) => {
                var format = new Zondy.Format.PolygonJSON();
                //将MapGIS要素JSON反序列化为ol.Feature类型数组
                var features = format.read(data);
                resolve(features);
            });
        });
    }

    static queryByFid({ fids, service }) {
        //初始化查询结构对象，设置查询结构包含几何信息
        var queryStruct = new Zondy.Service.QueryFeatureStruct();
        queryStruct.IncludeGeometry = true;

        //var objectIds = "10,104,185,238";
        //实例化查询参数对象
        var queryParam = new Zondy.Service.QueryParameter({
            objectIds: fids,
            resultFormat: "json",
            struct: queryStruct,
        });
        //设置查询分页号
        queryParam.pageIndex = 0;
        //设置查询要素数目
        queryParam.recordNumber = 20;
        //实例化地图文档查询服务对象
        var queryService = new Zondy.Service.QueryDocFeature(
            queryParam,
            service.docName,
            service.layerId,
            mapServer,
        );
        return new Promise((resolve, reject) => {
            //执行查询操作，
            queryService.query((data) => {
                console.log(data);
                resolve(data);
            });
        });
    }

    /**
     * 根据属性查询
     * @param options
     * @param options.where = {
     *     key, value
     * } 查询条件数组
     * @param options.service = {
     *     docName: "xzd2404", //地图文档名称
     *     layerId: 3 //图层索引
     * } 地图文档查询服务对象
     */
    static queryByAttribute({ where, service }) {
        //初始化查询结构对象，设置查询结构包含几何信息
        var queryStruct = new Zondy.Service.QueryFeatureStruct();
        queryStruct.IncludeGeometry = true;
        //实例化查询参数对象
        var queryParam = new Zondy.Service.QueryParameter({
            resultFormat: "json",
            struct: queryStruct,
        });
        //设置查询分页号
        queryParam.pageIndex = 0;
        //设置查询要素数目
        queryParam.recordNumber = 20;
        //根据输入框内容设置查询条件
        // queryParam.where = `name='${val}'`;
        //查询name属性包含输入框内容的要素
        queryParam.where = `${where.key} like '%${where.value}%'`;

        //实例化地图文档查询服务对象
        var queryService = new Zondy.Service.QueryDocFeature(
            queryParam,
            service.docName,
            service.layerId,
            mapServer,
        );
        //执行查询操作，querySuccess为查询回调函数
        return new Promise((resolve) => {
            queryService.query((data) => {
                var format = new Zondy.Format.PolygonJSON();
                //将MapGIS要素JSON反序列化为ol.Feature类型数组
                var features = format.read(data);
                resolve(features);
            });
        });
    }
}
export { Query };
